#include "stringtokenizer.h"

StringTokenizer::StringTokenizer()
{
	stream="";
	current=0;
	length=0;
}

StringTokenizer::StringTokenizer(string str)
{
	stream = str;
	current=0;
	length=stream.length();
}

bool StringTokenizer::currentIsCharacter()
{
	return (((int)stream[current]>=(int)'a')&&((int)stream[current]<=(int)'z'))||
		((int)stream[current]>=(int)'A'&&(int)stream[current]<=(int)'Z')||
		((int)stream[current]>=(int)'0'&&(int)stream[current]<=(int)'9')||
		((int)stream[current]==(int)';')||
		((int)stream[current]==(int)'(')||
		((int)stream[current]==(int)')')||
		((int)stream[current]==(int)'=')||
		((int)stream[current]==(int)'+')||
		((int)stream[current]==(int)'-')||
		((int)stream[current]==(int)'*')||
		((int)stream[current]==(int)'{')||
		((int)stream[current]==(int)'}')||
		((int)stream[current]==(int)'_')||
		((int)stream[current]==(int)'"')||
		((int)stream[current]==(int)'/');
}

bool StringTokenizer::endOfStream()
{
	return (current>=length);
}

StringTokenizer::~StringTokenizer()
{
}

string StringTokenizer::nextToken()
{
	//loop to ignore all other characters
	string empty = "";
	if (endOfStream())
		return empty;
	while (!endOfStream()&&!currentIsCharacter())
		current++;

	if (endOfStream())
		return empty;

	int traverse = current;

	char c = stream[current];

	if ((int)c==(int)';'||
		(int)c==(int)'('||
		(int)c==(int)')'||
		(int)c==(int)'='||
		(int)c==(int)'+'||
		(int)c==(int)'-'||
		(int)c==(int)'*'||
		(int)c==(int)'{'||
		(int)c==(int)'}'||
		(int)c==(int)'_'||
		(int)c==(int)'"'||
		(int)c==(int)'/')
	{
		string c1 = stream.substr(current,1);
		current++;
		return c1;
	}
	else
	{
		while ((traverse<length)&&(((int)stream[traverse]>=(int)'a'&&(int)stream[traverse]<=(int)'z')
			||((int)stream[traverse]>=(int)'A'&&(int)stream[traverse]<=(int)'Z')
			||((int)stream[traverse]>=(int)'0'&&(int)stream[traverse]<=(int)'9')))
		{
			traverse++;
		}
		string result = stream.substr(current,traverse-current);

		current=traverse;
		return result;
	}
}